<template>
  <div>
    <!-- 导航栏区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" class="bgbootm" @click="showCateDialog">添加分类</el-button>
      <!-- 树形展开结构 -->
      <tree-table class="bgbootm" :data='catesList' :columns="columns" :selection-type='false' :expand-type='false'
        :show-row-hover="false" show-index border>
        <!-- 是否有效 -->
        <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" size="mini" class="el-icon-edit" @click="showEditDialog(scope.row.cat_id)"> 编辑
          </el-button>
          <el-button type="danger" size="mini" class="el-icon-delete" @click="delCates(scope.row.cat_id)"> 删除
          </el-button>
        </template>
      </tree-table>
      <!-- 分页功能 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </el-card>
    <!-- 添加分类会话框 -->
    <el-dialog title="添加商品分类" :visible.sync="cateDialogVisible" width="50%" @close="cateDialogReset">
      <el-form :model="cateForm" :rules="cateFormRules" ref="cateFormRef" label-width="80px">
        <el-form-item label="分类名称" prop="cat_name" class="is-required">
          <el-input v-model="cateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="cat_level">
          <el-cascader :options="getCatesList" v-model="selectParent" :props="eascaderList" clearable
            @change="cateParentChange" placeholder="请选择">
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateForm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑会话框 -->
    <el-dialog title="修改分类" :visible.sync="editDialogVisible" @close="reseteditForm" width="50%">
      <el-form :model="cateForm" :rules="cateFormRules" ref="cateFormRef" label-width="80px">
        <el-form-item label="分类名称" prop="cat_name" class="is-required">
          <el-input v-model="cateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCateInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data () {
    return {
      catesList: [],  // 商品分类列表
      queryInfo: {
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      // 为table指定列
      columns: [
        {
          prop: "cat_name",
          label: '分类名称'
        },
        {
          label: '是否有效',
          // 定义模板列
          type: "template",
          // 模板列命名
          template: "isOk"
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      cateDialogVisible: false,
      cateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0,
        cat_id: ''
      },
      cateFormRules: {
        cat_name: [
          { require: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 父级分类的列表
      getCatesList: [],
      // 选中父级的ID
      selectParent: [],
      // 级联选择器的配置
      eascaderList: {
        value: "cat_id",
        children: 'children',
        label: 'cat_name',
        expandTrigger: 'hover',
        checkStrictly: 'true'
      },
      editDialogVisible: false,
    }
  },
  methods: {
    // 监听编辑提交
    reseteditForm () {
      this.$refs.cateFormRef.resetFields()
      this.selectParent = []
      this.cateForm.cat_level = 0
      this.cateForm.cat_pid = 0
    },
    // 编辑提交角色
    editCateInfo () {
      this.$refs.cateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('categories/' + this.cateForm.cat_id, {
          cat_id: this.cateForm.cat_id,
          cat_level: this.cateForm.cat_level,
          cat_name: this.cateForm.cat_name
        })
        if (res.meta.status !== 200) return this.$message.error('获取数据失败')
        this.getCateList()
        this.editDialogVisible = false
        this.$message.success(res.meta.msg)
      })
    },
    //删除
    async delCates (id) {
      console.log(id);
      const confirmResult = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err);
      if (confirmResult !== "confirm") {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除用户失败')
      }
      this.$message.success('删除用户成功')
      this.getCateList()
    },
    // 编辑
    async showEditDialog (id) {
      const { data: res } = await this.$http.get('categories/' + id)
      if (res.meta.status !== 200) return this.$message.error('更新失败')
      this.cateForm = res.data
      this.editDialogVisible = true
    },
    // 监听重置会话框
    cateDialogReset () {
      this.$refs.cateFormRef.resetFields()
      this.selectParent = []
      this.cateForm.cat_level = 0
      this.cateForm.cat_pid = 0
    },
    // 选择项发生变化触发函数
    cateParentChange () {
      // console.log(this.selectParent);
      // 如果selectParent的长度是大于0的 则有选中的父级
      if (this.selectParent.length > 0) {
        // 父级分类的id
        this.cateForm.cat_pid = this.selectParent[this.selectParent.length - 1]
        this.cateForm.cat_level = this.selectParent.length
      } else {
        this.cateForm.cat_pid = 0
        // 当前分类的等级赋权
        this.cateForm.cat_level = 0
      }
    },
    addCateForm () {
      this.$refs.cateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.cateForm)
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.getCateList()
        this.$message.success(res.meta.msg)
        this.cateDialogVisible = false
      })
    },
    async getCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.catesList = res.data.result
      this.total = res.data.total
    },
    // 请求父级数据
    async getParentsCate () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.getCatesList = res.data
    },
    // 会话框
    showCateDialog () {
      this.getParentsCate()
      this.cateDialogVisible = true
    },
    // 分页
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getCateList()
  },
  mounted () { }
}
</script>

<style lang='less' scoped>
.bgbootm {
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
</style>